package net.starwifi.duobao.utils.kd;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import net.starwifi.duobao.utils.HttpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/23.
 */
public class KDHelper {

    //username:xiongxiangzz,pass:@xiongxiangzz

    //电商ID
    private static String EBusinessID="1255539";
    //电商加密私钥，快递鸟提供，注意保管，不要泄漏
    private static String AppKey="7b35ee6a-f9a6-4aba-98ed-5c1aaef1163c";
    //请求url
    private static String ReqURL="http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";


    public static String getJson(String sn){
        String json="";
        try {
            String requestData= "{'OrderCode':'','ShipperCode':'SF','LogisticCode':'"+sn+"'}";
             Map<String, Object> params = new HashMap<String, Object>();
            params.put("RequestData", urlEncoder(requestData, "UTF-8"));
            params.put("EBusinessID", EBusinessID);
            params.put("RequestType", "1002");
            String dataSign=encrypt(requestData, AppKey, "UTF-8");
            params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");
             json=HttpUtils.post("http://api.kdniao.cc:8081/Ebusiness/EbusinessOrderHandle.aspx", params);
            System.out.println(json);
        }catch (Exception e){
            e.printStackTrace();
        }
       return  json;
    }


    public static KuaiDiDTO getEntity(String sn){
        String json=getJson(sn);
        if(json!=null&&!json.equals("")){
            return KuaiDiDTO.parse(json);
        }else{
            return null;
        }
    }


    /**
     * MD5加密
     * @param str 内容
     * @param charset 编码方式
     * @throws Exception
     */
    @SuppressWarnings("unused")
    private static String MD5(String str, String charset) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes(charset));
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer(32);
        for (int i = 0; i < result.length; i++) {
            int val = result[i] & 0xff;
            if (val <= 0xf) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }

    /**
     * base64编码
     * @param str 内容
     * @param charset 编码方式
     * @throws UnsupportedEncodingException
     */
    private static String base64(String str, String charset) throws UnsupportedEncodingException{
        String encoded = Base64.encode(str.getBytes(charset));
        return encoded;
    }

    @SuppressWarnings("unused")
    private static String urlEncoder(String str, String charset) throws UnsupportedEncodingException{
        String result = URLEncoder.encode(str, charset);
        return result;
    }

    /**
     * 电商Sign签名生成
     * @param content 内容
     * @param keyValue Appkey
     * @param charset 编码方式
     * @throws UnsupportedEncodingException ,Exception
     * @return DataSign签名
     */
    @SuppressWarnings("unused")
    private static String encrypt (String content, String keyValue, String charset) throws UnsupportedEncodingException, Exception
    {
        if (keyValue != null)
        {
            return base64(MD5(content + keyValue, charset), charset);
        }
        return base64(MD5(content, charset), charset);
    }


}
